<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class LoginController extends Controller
{
    public function index()
    {
    	// 已登录直接登录
    	if (session('admin_id')) {
            $this->redirect('admin/index/index');
        }
    	
    	return $this->fetch();
    }
    
    
    
        /**
     * 登录验证
     * @return string
     */
    public function login() {
        if(request()->isPost()){
       $data            = request()->only(['admin_name', 'admin_pwd']);
       if(!captcha_check(input('verify'))){//验证码判断
 		return toError(408,'验证码错误');
				}
		$data['admin_pwd']=md5_salt($data['admin_pwd']);
		$user=Db::name('auth_user')->where('admin_name',$data['admin_name'])->find();
		if($user){
				session('admin_id', $user['id']);
                session('admin_name', $user['admin_name']);
                session('ip', $this->request->ip());
			if(Db::name('auth_user')->where($data)->find()){
				$user=Db::name('auth_user')->where($data)->find();
				if($user['status'] != 1){
				$text='<span style="color:red">用户【'.$user['admin_name'].'】用户已被禁用，登录失败</span>';
            	$status='<span style="color:red">操作失败</span>';
            	logc($text,$status);
					return toError(408,'用户已被禁用');
				}
                session('thumb', $user['thumb']);
				$text='用户【'.$user['admin_name'].'】登录';
            	$status='操作成功';
            	logc($text,$status);
			return	toSuccess('登录成功','index/index');
				}else{
				$text='<span style="color:red">用户【'.$user['admin_name'].'】登录失败，密码错误</span>';
            	$status='<span style="color:red">操作失败</span>';
            	logc($text,$status);
				
					return toError(408,'账户名或密码错误');
			}
		}else{
			return toError(408,'用户不存在');
			}
			
        }
           
    }
    
       /**
     * 退出登录
     */
    public function logout() {
        session('admin_id', null);
        session('admin_name', null);
        $this->success('退出成功', 'admin/login/index');
    } 
    
    
    
    
    
    
    


}
